package com.mapull.example.section1;

/**
 * @author: mapull
 * @description: Convert Java String to int
 * @date:2020/04/16
 **/
public class StringToInt {

    public static void main(String[] args) {
        method3();
    }

    public static void method1() {
        Integer.parseInt(null);
    }

    public static void method2() {
        Integer.parseInt("");
    }

    public static void method3() {
        Integer.parseInt("AF");
    }

    public static void method4() {
        int intVal = Integer.parseInt("101");
        System.out.println(intVal);
    }

    public static void method5() {
        int intVal = Integer.parseInt("101", 8);
        System.out.println(intVal);
    }

    public static void method6() {
        int intVal = Integer.parseInt("FF", 16);
        System.out.println(intVal);
    }

    public static void method7() {
        // 十进制
        Integer intVal = Integer.valueOf("101");
        System.out.println(intVal);
        // 八进制
        Integer intVal1 = Integer.valueOf("101", 8);
        System.out.println(intVal1);
        // 十六进制
        Integer intVal2 = Integer.valueOf("AF", 16);
        System.out.println(intVal2);
    }

    public static void method8() {
        // 十进制
        Integer intVal = Integer.decode("+100");
        System.out.println(intVal);
        // 八进制
        Integer intVal1 = Integer.decode("+0100");
        System.out.println(intVal1);
        // 十六进制
        Integer intVal2 = Integer.decode("+0x100");
        System.out.println(intVal2);
    }

    public static void method9() {
        String str = "1001x";
        int intVal = 0;

        try {
            intVal = Integer.parseInt(str);
        } catch (NumberFormatException nfe) {
            System.out.println("无法转换字符串：" + str);
        }
        System.out.println(intVal);
    }


}
